package com.movie.controller.SysCustomerController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.movie.beans.Const.Const;
import com.movie.beans.SysUser;
import com.movie.beans.SysUserRole;
import com.movie.beans.base.AjaxResult;
import com.movie.service.SysPasswordService;
import com.movie.service.SysUserRoleService;
import com.movie.service.UserService;
import com.movie.utils.DateUtils;
import com.movie.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/customer")
public class SysCustomerController {
    @Autowired
    private UserService userService;

    @Autowired
    private SysUserRoleService userRoleService;

    @Autowired
    private SysPasswordService sysPasswordService;
    @Autowired
    private SysPasswordService passwordService;


    private static String prefix = "/system/customer";
    @GetMapping()
    public String index(){
        return prefix+"/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public Map list(SysUser sysUser,Integer limit, Integer page){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<Long> ids = userRoleService.selectUserByRoleId(2L);
        List<SysUser> users = new ArrayList<>();
        for (Long id:ids) {
            SysUser u = userService.selectUserById(id);
            users.add(u);
        }
        map.put("msg","");
        map.put("code",0);
        map.put("data",users);
        map.put("count",new PageInfo(ids).getTotal());
        return map;
    }

    @PostMapping("/search")
    @ResponseBody
    public Map search(@RequestParam String searchVal,Integer limit, Integer page){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<SysUser> users = userService.selectUserBySearchVal(searchVal);
        map.put("msg","");
        map.put("code",0);
        map.put("data",users);
        map.put("count",new PageInfo(users).getTotal());
        return map;
    }

    @PostMapping("/insert")
    @ResponseBody
    public AjaxResult insert(SysUser sysUser){
        if(userService.selectUserByName(sysUser)==null){
            sysUser.setSalt(ShiroUtil.randomSalt());
            sysUser.setStatus(Const.STATUS_NORMAL);
            sysUser.setPassword(sysPasswordService.encryptPassword(sysUser.getLoginName(),sysUser.getPassword(),sysUser.getSalt()));
            userService.insert(sysUser);
            SysUserRole userRole = new SysUserRole(2L,sysUser.getUserId());
            userRoleService.insert(userRole);
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }

    }
    @PostMapping(value = "/edit")
    @ResponseBody
    public AjaxResult edit(SysUser sysUser){
        sysUser.setUpdateTime(DateUtils.getNowDate());
        if (userService.updateUser(sysUser) > 0)
        {
            return AjaxResult.success("更新信息成功");
        }
        return AjaxResult.error("更新信息异常，请联系管理员");
    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable(value = "id")Long id){
        if (userService.remove(id) > 0)
        {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除异常，请联系管理员");
    }
    @PostMapping("/resetPwd/{id}")
    @ResponseBody
    public AjaxResult resetPwd(@PathVariable(value = "id")Long id){
        SysUser user = userService.selectUserById(id);
        user.setSalt(ShiroUtil.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), Const.DEFAULT_PASSWORD, user.getSalt()));
        user.setPwdUpdateTime(DateUtils.getNowDate());
        if (userService.resetUserPwd(user) > 0)
        {
            ShiroUtil.setSysUser(userService.selectUserById(user.getUserId()));
            return AjaxResult.success("修改密码成功");
        }
        return AjaxResult.error("修改密码异常，请联系管理员");
    }

}
